var jwt = require("jsonwebtoken");

var secretKey = "kevin1016";

var tokenShortAll = {
  en(str, time = "10s") {
    return jwt.sign(str, secretKey, { expiresIn: time });
  },
  de(tokens) {
    try {
      return {
        code: 200,
        data: jwt.verify(tokens, secretKey),
      };
    } catch (error) {
      console.log(error);
      if (JSON.stringify(error).includes("jwt expired")) {
        return {
          code: 300,
          data: true,
          msg: "jwt expired",
        };
      } else {
        return {
          code: 405,
          data: false,
          msg: "SyntaxError",
        };
      }
    }
  },
};

module.exports = { tokenShortAll };
